package cn.net.autocode.platform.providers;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.TreeUtils;
import cn.net.autocode.core.utils.UserThreadLocalUtils;
import cn.net.autocode.dbManager.SqlWrapper;
import cn.net.autocode.dbManager.SqlWrapperBuilder;
import cn.net.autocode.platform.exception.PlatformQueryException;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("autocode")
@Slf4j
public class QueryProviderCtrl extends ProviderCtrl{


    private void resultDeal(JSONArray results,String  tableCode){
        JSONArray rules = dictDao.queryJSONArray("select ac_table_rules.* from ac_table_rules,ac_table where rule_type in (41) and ac_table_rules.table_id=ac_table.id and ac_table.code=?", tableCode);
        providerServ.resultDeal(results,rules);
    }

    private SqlWrapperBuilder swbInit(String tableCode, JSONObject data) throws PlatformQueryException {
        if (!data.containsKey("orgCode")||data.getString("orgCode")==null||data.getString("orgCode").isBlank()){
            data.put("orgCode", UserThreadLocalUtils.getOrgCode());
        }
        String[] tables = tableCode.split("\\.");
        SqlWrapperBuilder swb;
        JSONObject table;
        if(tables.length==2){
            table = this.tableMsg(tables[1]);
            JSONObject refTable  = this.tableMsg(tables[0]);
            swb = providerServ.existsInit(table,refTable,data);
        }else{
            table = this.tableMsg(tableCode);
            //查询参数处理
            swb = providerServ.queryInit(table, data);
        }
        return swb;
    }

    private String mainTable(String tableCode){
        String[] tables = tableCode.split("\\.");
        String  tableId;
        if(tables.length==2){
            tableId = tables[1];

        }else if (tables.length==1){
            tableId = tableCode;
        }else{
            throw new RuntimeException("传递的tableCode参数有误！");
        }
        return tableId;
    }


    @RequestMapping("queryArray/{tableCode}")
    public ResultT queryArray(@PathVariable("tableCode") String tableCode, @RequestBody JSONObject data){
        try {
            String mainTableCode = this.mainTable(tableCode);
            SqlWrapperBuilder swb = this.swbInit(tableCode,data);
            SqlWrapper sw = swb.sqlWrapper();
            JSONArray result = dao.queryJSONArray(sw.getSql(),sw.getParams().toArray());
            this.resultDeal(result, mainTableCode);
            return ResultT.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }




    @RequestMapping("queryTreeArray/{tableCode}")
    public ResultT queryTreeArray(@PathVariable("tableCode") String tableCode, @RequestBody JSONObject data){
        try {
            String mainTableCode = this.mainTable(tableCode);
            SqlWrapperBuilder swb = this.swbInit(tableCode,data);
            SqlWrapper sw = swb.sqlWrapper();
            JSONArray result = dao.queryJSONArray(sw.getSql(),sw.getParams().toArray());
            this.resultDeal(result, mainTableCode);
            return ResultT.success(TreeUtils.commonTree(result));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }




    @RequestMapping("queryPagingArray/{tableCode}")
    public ResultT queryPagingArray(@PathVariable("tableCode") String tableCode, @RequestBody JSONObject pagingParam){

        try {
            JSONObject page = pagingParam.getJSONObject("pagingData");
            JSONObject data = pagingParam.getJSONObject("param");

            String mainTableCode = this.mainTable(tableCode);
            SqlWrapperBuilder swb = this.swbInit(tableCode,data);

            SqlWrapper sw = swb.sqlWrapper();
            SqlWrapper countSw = swb.countSqlWrapper();

            dao.pageInfo(page, countSw.getSql(),countSw.getParams().toArray());
            JSONArray dataArray = dao.queryJSONArrayByPage(sw.getSql(),page,sw.getParams().toArray());

            this.resultDeal(dataArray, mainTableCode);

            JSONObject result = new JSONObject();
            result.put("pagingData",page);
            result.put("data",dataArray);
            return ResultT.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @RequestMapping("queryObject/{tableCode}")
    public ResultT queryObject(@PathVariable("tableCode") String tableCode, @RequestBody JSONObject data){
        try {
            String mainTableCode = this.mainTable(tableCode);
            SqlWrapperBuilder swb = this.swbInit(tableCode,data);
            SqlWrapper sw = swb.sqlWrapper();
            JSONObject result = dao.queryJSON(sw.getSql(),sw.getParams().toArray());
            if(!result.isEmpty()){
                JSONArray array = new JSONArray();
                array.add(result);
                this.resultDeal(array, mainTableCode);
            }
            return ResultT.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


}
